﻿/****************************************************************************
 * Tutorial: CDCase_Dictionary Test Class: Testing the Dictionary DataType
 * File: CDCase_Dictionary.as
 * 
 * Description: This is a rough benchmark test to create 100,000 Objects, add them to a Dictionary
 * 				search for a specific object with index 90,000 , and clean up
 * 				displaying both Time to complete and System Memory used
 *
 * @author:  Haelix @ Zombie Flambe 
 * @site:    http://www.zombieflambe.com
 * @email:   haelix@zombieflambe.com
 * @version: 1.0
 * @created: 1st January 2009
 ****************************************************************************/

package zombieflambe {
	import flash.utils.getTimer;
	import flash.display.Sprite;
	import flash.net.LocalConnection;
	import flash.system.System;
	import flash.utils.Dictionary;
	
	public class CDCase_Dictionary extends Sprite {
		// Get TotalMemory before most everything
		private var beginMem:Number = System.totalMemory;
		
		public var caseDict:Dictionary;
		private var caseObj:Object;
		private const MAX_CDS:int = 100000;
		private const MY_CD_TITLE:String = "cdTitle_93456";

		public function CDCase_Dictionary() {
			
			var startTime:Number 	,  endTime:Number 		, totalTime:Number;
			var endCreate:Number 	,  totalCreate:Number;
			var startSearch:Number ,  endSearch:Number 	, totalSearch:Number;
			var fullMem:Number  ,  endMem:Number;
			
			startTime = getTimer();
			
			caseDict = new Dictionary( true );
			
			var myCD = new Object();
			myCD.cdTitle = MY_CD_TITLE;
			
			
			// Initialize all our cases
			var i:int;
			for( i = 0; i < MAX_CDS; i++ )  {
				var tmpCD:Object = new Object();
				tmpCD.cdTitle = "cdTitle_" + i;
	
				// This lets us hold onto the reference of the tmp cd
				if( tmpCD.cdTitle == myCD.cdTitle )  
					myCD = tmpCD;
				caseDict[ tmpCD ] = tmpCD.cdTitle;				
			}

			endCreate = getTimer();
			
			// Get TotalMemory After Adding 100,000 array
			fullMem = System.totalMemory;
			
			startSearch = getTimer();
						
			var result:Boolean 		= searchDictionary( myCD );
			
			endSearch 	= getTimer();			
			
			clean();
			
			delete tmpCD.cdTitle;
			delete myCD.cdTitle;
			
			tmpCD = null;
			myCD  = null;
			
			endTime = getTimer();
			
			totalCreate = ( endCreate - startTime 	) / 1000;
			
			totalSearch = ( endSearch - startSearch ) / 1000;
			
			totalTime 	= ( endTime   - startTime   ) / 1000;
		
			/**********************************
			 * Unsupported Way to Force GC
			 * 	 From Grant Skinner's Series on Resource Management
			 *   
			 *   
			 *   From:  http://www.gskinner.com/blog/archives/2006/08/as3_resource_ma_2.html
			 *****************************/
			try {
			   new LocalConnection().connect('foo');
			   new LocalConnection().connect('foo');
			} catch (e:*) {}
			
			// Get TotalMemory After Forcing GC
			endMem = System.totalMemory;
			
/** ** RESULTS ** **/
			
			trace( "Dictionary Create/Find/Clean Test | MaxCDs: " + MAX_CDS );
			trace( "******************************************************" );
			trace( "*   Search Outcome, false if Not Found: " + result 		);
			trace( "******************************************************" );
			trace( "******************************************************" );
			trace( "* CREATE TIME TOTALS                                  " );
			trace( "******************************************************" );
			trace( "* Start Create Time: " + startTime        				);
			trace( "* End   Create Time: " + endCreate        				);
			trace( "* "														);
			trace( "* Total Create Time: " + totalCreate        			);
			trace( "******************************************************" );
			trace( "* SEARCH TIME TOTALS                                  " );
			trace( "******************************************************" );
			trace( "* Start Search Time: " + startSearch        			);
			trace( "* End   Search Time: " + endSearch        				);
			trace( "* "														);
			trace( "* Total Search Time: " + totalSearch        			);
			trace( "******************************************************" );
			trace( "******************************************************" );
			trace( "* TIME TOTALS                                         " );
			trace( "******************************************************" );
			trace( "* Total Start Time  : " + startTime        				);
			trace( "* Total End Time    : " + endTime        				);
			trace( "* "														);
			trace( "* Total Time        : " + totalTime        				);
			trace( "******************************************************" );
			trace( "******************************************************" );
			trace( "* MEMORY TOTALS ( each size was  / 1024 )             " );
			trace( "******************************************************" );
			trace( "* Beginning Memory   : " + beginMem / 1024  			);
			trace( "* Full Memory        : " + fullMem  / 1024  			);
			trace( "* Ending Memory      : " + endMem   / 1024  			);
			trace( "* "														);
					var whileFull  = ( ( fullMem - beginMem ) / 1024  		);
					var afterEmpty = ( ( endMem  - beginMem ) / 1024  		);
			trace( "* Total While Full   : " + whileFull        			);
			trace( "* Total After Empty  : " + afterEmpty 	  				);
			
		}
		
		private function searchDictionary( myCD:Object ):Boolean  {
			var titleFound:Boolean = false;
			if( caseDict[ myCD ] )
				titleFound = true;
			
			return titleFound;
		}
		
		private function clean():void  {
			var i:int = MAX_CDS - 1;
			while( i >= 0  )  {
				delete caseDict[ "cdTitle_" + i ];
				i--;
			}
			i=0;
			caseDict = null;
		}
	}
}